package com.jshx.sz.gdjt.util.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

import android.util.Log;

public class FileUploadUtils {

	private String TAG = "FileUploadUtils";
	
	public FileUploadUtils()
	{
	}
	
	public FileUploadUtils(String url, String path)
	{
		uploadUrl = url;
		filePath = path;
	}
	private final int K = 1024;
	private int blockSize = 4 * K;
	
	public int getBlockSize() {
		return blockSize / K;
	}

	/*
	 * 设置每次上传大小，单位为KB, 默认为4K
	 */
	public void setBlockSize(int blockSize) {
		this.blockSize = blockSize * K;
	}
	
	private String uploadUrl;
	
	public String getUploadUrl() {
		return uploadUrl;
	}

	public void setUploadUrl(String uploadUrl) {
		this.uploadUrl = uploadUrl;
	}
	
	private String filePath;
	
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	/**
	 * 上传文件到指定路径，上传成功返回Response的Header,否则返回null
	 */
	public Map<String, List<String>> upload() throws Exception {
		if("".equals(uploadUrl) || uploadUrl == null ||
		   "".equals(filePath) || filePath == null)
		{
			throw(new Exception("parameter is NOT correct"));
		}
		
		File file = new File(filePath);
		long len = file.length();
		
		FileInputStream fis = new FileInputStream(file);
		
		byte[] buff = new byte[blockSize];
		
		HttpURLConnection connection = null;
		Map<String, List<String>> headers = null;
		OutputStream os = null;
		String BOUNDARY="------123123123123123123";
		try {
			URL url = new URL(uploadUrl);
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestProperty("Connection", "Keep-Alive");
			connection.setRequestProperty("Cache-Control", "no-cache");
			connection.setRequestProperty("Accept", "*/*");
			//connection.setRequestProperty("Content-Type", "text/html");
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+BOUNDARY);
			
			connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
			connection.setRequestProperty("Accept-Language", "zh-cn");
			connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
			connection.setRequestProperty("Content-Length", String.valueOf(len));
			connection.setConnectTimeout(20 * 1000);
			
			//设置读取超时，避免在网络异常的情况下，可能会导致程序僵死
			connection.setReadTimeout(20 * 1000);
			System.setProperty("sun.net.client.defaultConnectTimeout", "20000");
			System.setProperty("sun.net.client.defaultReadTimeout", "20000");
			connection.setDoOutput(true);
			connection.connect();
			
			os = connection.getOutputStream();
			
			// 开始分段发送数据
			int readSize = -1;
			while((readSize = fis.read(buff, 0, blockSize)) > 0)
			{
				os.write(buff, 0, readSize);
			}
			
			//这里返回的是消息头里的信息
			if (connection.getResponseCode() == 200) {
				
				headers = connection.getHeaderFields();
			}
			
			Log.d(TAG, "Response Code:" + connection.getResponseCode());
			
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} 
		finally {
			
			if(os != null)
			{
				os.close();
			}
			
			//关闭连接
			if (connection != null) {
				connection.disconnect();
			}
			
			if(fis != null)
			{
				fis.close();
			}
		}
		
		return headers;
	}
}
